<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增课程管理')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-course-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">选择分类：</label>
            <div class="col-sm-8">
                <select id="subjectIds" class=" form-control select2-multiple" multiple>
                    <th:block th:each="sub:${subjectList}" th:object="${sub}">
                        <option th:value="*{id}" th:text="*{subjectName}"></option>
                    </th:block>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">课程标题：</label>
            <div class="col-sm-8">
                <input name="courseName" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">课程副标题：</label>
            <div class="col-sm-8">
                <input name="courseSubtitle" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">课程简介：</label>
            <div class="col-sm-8">
                <input type="hidden" class="form-control" name="courseIntro">
                <div class="summernote" id="courseIntro"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">课程难度：</label>
            <div class="col-sm-8">
                <select name="courseDifficulty" class="form-control m-b"
                        th:with="type=${@dict.getType('course_difficulty')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                            th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">商品价格：</label>
            <div class="col-sm-2">
                <div class="radio-box" th:each="dict : ${@dict.getType('course_if_discount')}">
                    <input type="radio" th:id="${'courseIfDicount_' + dict.dictCode}" name="courseIfDicount"
                           th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${'courseIfDicount_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>

            <label class="col-sm-1 control-label">原价：</label>
            <div class="col-sm-1">
                <input name="courseOriginalPrice" class="form-control" type="text">
            </div>

            <label class="col-sm-1 control-label">折后价：</label>
            <div class="col-sm-1">
                <input name="courseDicountPrice" class="form-control" type="text">
            </div>

        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">打折描述：</label>
            <div class="col-sm-8">
                <textarea name="courseDiscountRemark" class="form-control"></textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">排序：</label>
            <div class="col-sm-8">
                <input name="courseSort" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否轮播：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                    <input type="radio" th:id="${'courseIfShuffling_' + dict.dictCode}" name="courseIfShuffling"
                           th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${'courseIfShuffling_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否精品推荐：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                    <input type="radio" th:id="${'courseIfBoutique_' + dict.dictCode}" name="courseIfBoutique"
                           th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${'courseIfBoutique_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否免费：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('course_if_free')}">
                    <input type="radio" th:id="${'courseIfFree_' + dict.dictCode}" name="courseIfFree"
                           th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${'courseIfFree_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否对外：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('course_if_foreign')}">
                    <input type="radio" th:id="${'courseIfForeign_' + dict.dictCode}" name="courseIfForeign"
                           th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${'courseIfForeign_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">封面：</label>
            <div class="col-sm-8">
                <input type="hidden" name="courseCover">
                <div class="file-loading">
                    <input class="form-control file-upload" id="courseCover" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">轮播图：</label>
            <div class="col-sm-8">
                <input type="hidden" name="courseShuffling">
                <div class="file-loading">
                    <input class="form-control file-upload" id="courseShuffling" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">pc端轮播图：</label>
            <div class="col-sm-8">
                <input type="hidden" name="coursePcShuffling">
                <div class="file-loading">
                    <input class="form-control file-upload" id="coursePcShuffling" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否展示详情：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                    <input type="radio" th:id="${'courseIfDetail_' + dict.dictCode}" name="courseIfDetail"
                           th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${'courseIfDetail_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">播放量：</label>
            <div class="col-sm-8">
                <input name="courseViewCount" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">上线状态：</label>
            <div class="col-sm-8">
                <select name="courseOnlineState" class="form-control m-b"
                        th:with="type=${@dict.getType('course_online_status')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                            th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea name="remark" class="form-control"></textarea>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<script th:inline="javascript">
    var prefix = ctx + "course/course"
    var subjectIds = []
    $("#form-course-add").validate({
        focusCleanup: true
    });

    //普通表单提交
    function submitHandler() {
        if ($.validate.form()) {
            add()
        }
    }

    //获取多选框的值
    $('#subjectIds').change(function (e) {
        let o = document.getElementById('subjectIds').getElementsByTagName('option');
        subjectIds = []
        for (let i = 0; i < o.length; i++) {
            if (o[i].selected) {
                subjectIds.push(o[i].value)
            }
        }
    })

    function add() {
        var formData = new FormData();
        formData.append('subjectIds', subjectIds.join(","));
        formData.append('courseName', $("[name=courseName]").val());
        formData.append('courseSubtitle', $("[name=courseSubtitle]").val());
        formData.append('courseIntro', $("[name=courseIntro]").val());
        formData.append('courseDifficulty', $("[name=courseDifficulty]").find("option:selected").val());
        formData.append('courseIfDicount', $("[name=courseIfDicount]:checked").val());
        formData.append('courseDiscountRemark', $("[name=courseDiscountRemark]").val());
        formData.append('courseOriginalPrice', $("[name=courseOriginalPrice]").val());
        formData.append('courseSort', $("[name=courseSort]").val());
        formData.append('courseDicountPrice', $("[name=courseDicountPrice]").val());
        formData.append('courseIfShuffling', $("[name=courseIfShuffling]:checked").val());
        formData.append('courseIfBoutique', $("[name=courseIfBoutique]:checked").val());
        formData.append('courseIfFree', $("[name=courseIfFree]:checked").val());
        formData.append('courseIfForeign', $("[name=courseIfForeign]:checked").val());
        formData.append('courseIfDetail', $("[name=courseIfDetail]:checked").val());
        formData.append('courseViewCount', $("[name=courseViewCount]").val());
        formData.append('courseOnlineState', $("[name=courseOnlineState]").find("option:selected").val());
        formData.append('remark', $("[name=remark]").val());
        if ($('#courseCover')[0].files[0] != undefined) {
            formData.append('courseCoverFile', $('#courseCover')[0].files[0]);
        }
        if ($('#courseShuffling')[0].files[0] != undefined) {
            formData.append('courseShufflingFile', $('#courseShuffling')[0].files[0]);
        }
        if ($('#coursePcShuffling')[0].files[0] != undefined) {
            formData.append('coursePcShufflingFile', $('#coursePcShuffling')[0].files[0]);
        }
        $.ajax({
            url: prefix + "/add",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                $.operate.successCallback(result);
            }
        });
    }

    //文件上传
    $(".file-upload").fileinput({
        maxFileCount: 1,
        autoReplace: true,
        allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
        showUpload: false, //是否显示上传按钮
        dropZoneTitle: "拖拽文件到这里 …" + `<br>` + "仅支持单文件上传",
    }).on('fileuploaded', function (event, data, previewId, index) {
        $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        console.log(data.response.url)
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
    })
    //
    //富文本
    $(function () {
        $('.summernote').summernote({
            lang: 'zh-CN',
            dialogsInBody: true,
            callbacks: {
                onChange: function (contents, $edittable) {
                    $("input[name='" + this.id + "']").val(contents);
                },
                onImageUpload: function (files) {
                    var obj = this;
                    var data = new FormData();
                    data.append("file", files[0]);
                    $.ajax({
                        type: "post",
                        url: ctx + "common/upload",
                        data: data,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $('#' + obj.id).summernote('insertImage', result.url);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function (error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });
    });
</script>
</body>
</html>